<?php

	include("../includes/connect_db.php");

	extract($_POST, EXTR_SKIP);

	$fullSerial = array();

	switch ($act) {
		case 'monthviewrestaurant':
			$sql_valid = "SELECT a.id FROM resto a 
			WHERE CONCAT(YEAR(a.tanggal),'-',MONTH(a.tanggal)) = '".$date."'
			AND a.id IN (SELECT idresto FROM nota WHERE is_paid = 1)";
			$query_valid = mysql_query($sql_valid);
			$count_valid = mysql_num_rows($query_valid);

			if ($count_valid>0) {
				// ------------cek jumlah hari--------------
				$exdate = explode("-",$date);
				$jumHari = cal_days_in_month(CAL_GREGORIAN, $exdate[1], $exdate[0]);

				// ------------push urutan hari-------------
				for ($m = 1; $m <= $jumHari ; $m++) {
					$arrDay = array();

					$arrDay['tanggal'] = $exdate[0]."-".$exdate[1]."-".$m;

					$sql = "SELECT DATE(a.tanggal) AS month, SUM(b.harga*b.jumlah) AS paid FROM resto a 
					LEFT JOIN resto_menu b ON b.idresto = a.id
					WHERE CONCAT(YEAR(a.tanggal),'-',MONTH(a.tanggal)) = '".$date."' AND DATE_FORMAT(a.tanggal, '%d') = '".$m."'
					AND a.id IN (SELECT idresto FROM nota WHERE is_paid = 1)";
					$query = mysql_query($sql);
					$data = mysql_fetch_array($query);

					$paid = 0;
					if ($data[paid] != '') {
						$paid = $data[paid];
					}

					$arrDay["amount"] = $paid;

					array_push($fullSerial, $arrDay);
				}
			}
			// print_r($fullSerial);
			break;

		case 'yearviewrestaurant':
			$sql = "SELECT MONTH(b.tanggal) AS month, SUM(a.harga*a.jumlah) AS amount FROM resto_menu a
			LEFT JOIN resto b ON b.id = a.idresto
			WHERE a.idresto IN (SELECT idresto FROM nota WHERE is_paid = 1 AND is_service_charge = 0) 
			AND YEAR(b.tanggal) = '".$date."' GROUP BY MONTH(b.tanggal)";
			$query = mysql_query($sql);
			$fullSerial = array(
				array('period' => 'January', 'amount' => 0),
				array('period' => 'February', 'amount' => 0),
				array('period' => 'March', 'amount' => 0),
				array('period' => 'April', 'amount' => 0),
				array('period' => 'May', 'amount' => 0),
				array('period' => 'June', 'amount' => 0),
				array('period' => 'July', 'amount' => 0),
				array('period' => 'August', 'amount' => 0),
				array('period' => 'September', 'amount' => 0),
				array('period' => 'October', 'amount' => 0),
				array('period' => 'November', 'amount' => 0),
				array('period' => 'December', 'amount' => 0)
			);
			while ($data = mysql_fetch_array($query)) {
				// $dataSerial["period"] = $data["month"];
				$period = $data["month"]-1;
				$fullSerial[$period]["amount"] = $data["amount"];
				// $dataSerial["amount"] = $data["amount"];
				// array_push($fullSerial, $dataSerial);
				// print_r($fullSerial);
			}
			break;

		case 'monthviewroom':
			$sql_valid = "SELECT DATE_FORMAT(a.checkout, '%Y-%c-%d') AS 'date_checkout', 
			DATE_FORMAT(a.checkout, '%M %d, %Y') AS 'date_tampil'
			FROM front_office a 
			WHERE a.is_paid = 1 AND CONCAT(YEAR(a.checkout),'-',MONTH(a.checkout)) = '".$date."' 
			GROUP BY DATE_FORMAT(a.checkout, '%Y-%c-%d') ASC";
			$query_valid = mysql_query($sql_valid);
			$count_valid = mysql_num_rows($query_valid);

			if ($count_valid>0) {
				// ------------cek jumlah hari--------------
				$exdate = explode("-",$date);
				$jumHari = cal_days_in_month(CAL_GREGORIAN, $exdate[1], $exdate[0]);

				// ------------push urutan hari-------------
				for ($m = 1; $m <= $jumHari ; $m++) {
					$arrDay = array();

					$arrDay['tanggal'] = $exdate[0]."-".$exdate[1]."-".$m;

					$sql = "SELECT SUM(a.real_paid) AS 'paid' FROM front_office a
					WHERE DATE_FORMAT(a.checkout, '%d') = ".$m." AND 
					CONCAT(YEAR(a.checkout),'-',MONTH(a.checkout)) = '".$date."' 
					AND a.is_paid = 1";
					$query = mysql_query($sql);
					$data = mysql_fetch_array($query);

					$paid = 0;
					if ($data[paid] != '') {
						$paid = $data[paid];
					}

					$dateDisplay = $exdate[0]."-".$exdate[1]."-".$m;

					$arrDay["amount"] = $paid;

					array_push($fullSerial, $arrDay);
				}
			}

			// print_r($fullSerial);

			break;

		case 'yearviewroom':
			$sql = "SELECT MONTH(a.checkout) AS month, (a.real_paid*a.banyak_orang*(DATEDIFF(a.checkout, a.checkin))) AS room, 
			SUM(b.jumlah*b.harga) AS service
			FROM front_office a LEFT JOIN service_charge b ON b.idfront_office = a.id
			WHERE a.is_paid = 1 AND YEAR(a.checkout) = '".$date."' GROUP BY MONTH(a.checkout) ASC";
			$query = mysql_query($sql);
			$count = mysql_num_rows($query);
			if ($count>0) {
				$query = mysql_query($sql);
				$fullSerial = array(
					array('period' => 'January', 'amount' => 0),
					array('period' => 'February', 'amount' => 0),
					array('period' => 'March', 'amount' => 0),
					array('period' => 'April', 'amount' => 0),
					array('period' => 'May', 'amount' => 0),
					array('period' => 'June', 'amount' => 0),
					array('period' => 'July', 'amount' => 0),
					array('period' => 'August', 'amount' => 0),
					array('period' => 'September', 'amount' => 0),
					array('period' => 'October', 'amount' => 0),
					array('period' => 'November', 'amount' => 0),
					array('period' => 'December', 'amount' => 0)
				);
				while ($data = mysql_fetch_array($query)) {
					// $dataSerial["period"] = $data["month"];
					$amount = $data["room"]+$data["service"];
					$period = $data["month"]-1;
					$fullSerial[$period]["amount"] = $amount;
					// $dataSerial["amount"] = $data["amount"];
					// array_push($fullSerial, $dataSerial);
					// print_r($fullSerial);
				}
			}
			break;

		case 'monthviewagent':
			// ------------cek jumlah hari--------------
			$exdate = explode("-",$date);
			$jumHari = cal_days_in_month(CAL_GREGORIAN, $exdate[1], $exdate[0]);

			// ------------push urutan hari-------------
			for ($m = 1; $m <= $jumHari ; $m++) {
				$fullMenu = array();
				$fullMenu['date'] = $exdate[0]."-".$exdate[1]."-".$m;

				$sql_menu = "SELECT a.id, a.nama, a.komisi FROM agent a";
				$query_menu = mysql_query($sql_menu);
				while ($baris_menu = mysql_fetch_array($query_menu)) {
					$sql_date_menu = "SELECT SUM(b.comission) AS bayar
					FROM front_office a
					LEFT JOIN agent_paid b ON b.id_agent = a.idagent AND a.id = b.id_fo
					WHERE a.idagent = ".$baris_menu[id]." AND a.is_paid = 1
					AND YEAR(a.checkout) = '".$exdate[0]."' AND MONTH(a.checkout) = '".$exdate[1]."' 
					AND DATE_FORMAT(a.checkout, '%d') = ".$m;
					// echo $sql_date_menu;
					$query_date_menu = mysql_query($sql_date_menu);
					$date_menu = mysql_fetch_array($query_date_menu);

					$bayar = 0;
					if($date_menu['bayar'] != ""){
						$bayar = $date_menu['bayar'];
					}
					$fullMenu[$baris_menu["id"]] = $bayar;
				}
				array_push($fullSerial, $fullMenu);
				// print_r($fullSerial);
			}
			break;

		case 'yearviewagent':
			// ------------push urutan bulan-------------
			for ($m = 1; $m <= 12 ; $m++) {
				$fullMenu = array();

				// $namaBulan = array("Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember");
				// $fullMenu['bulan'] = $namaBulan[$m-1];
				$fullMenu['date'] = $date."-".$m;
				// $fullMenu['bulan'] = $m;

				$sql_menu = "SELECT a.id, a.nama, a.komisi FROM agent a";
				$query_menu = mysql_query($sql_menu);
				while ($baris_menu = mysql_fetch_array($query_menu)) {
					$sql_date_menu = "SELECT SUM(b.comission) AS bayar
					FROM front_office a
					LEFT JOIN agent_paid b ON b.id_agent = a.idagent AND a.id = b.id_fo 
					WHERE a.idagent = ".$baris_menu[id]." AND a.is_paid = 1
					AND YEAR(a.checkout) = '".$date."' AND MONTH(a.checkout) = '".$m."'";
					$query_date_menu = mysql_query($sql_date_menu);
					$date_menu = mysql_fetch_array($query_date_menu);

					$bayar = 0;
					if($date_menu['bayar'] != ""){
						$bayar = $date_menu['bayar'];
					}
					$fullMenu[$baris_menu["id"]] = $bayar;
				}
				array_push($fullSerial, $fullMenu);
				// print_r($fullSerial);
			}
			break;
	}
	echo json_encode($fullSerial);

?>